/* 
 * File:   Cammer.cpp
 * Author: gustavo
 * 
 * Created on August 29, 2012, 12:02 AM
 */

#include "Cammer.hpp"
#include "../../capture/v4l_capture.hpp"

namespace imp {
	namespace app {

		bool Cammer::loop() {

			if (rgb_data == NULL) {
				return false;
			}

			v4li.get_v4l_capture().grab_and_fill_rgb((char *) rgb_data);
			
			iview.show(rgb_data, width, height, imp::gui::ImageView::RGB);
			
			return true;

		}

		Cammer::Cammer() {

			this->set_title("Cammer");

			Gtk::HBox * hbox = new Gtk::HBox(false, 2);
			this->add(*hbox);

			Gtk::Frame * frame = new Gtk::Frame(" Camera ");
			frame->add(iview);

			hbox->pack_start(*frame, true, true, 0);
			hbox->pack_start(v4li, true, true, 0);

			iview.set_size_request(320, 240);
			this->show_all();

			rgb_data = NULL;

			v4li.signal_start().connect(sigc::mem_fun(*this, &Cammer::event_v4li_start));

		}

		void Cammer::event_v4li_start(bool start) {
			if (start) {

				v4li.get_v4l_capture().get_image_size(&width, &height);
				rgb_data = mem_allocn(width * height * 3, unsigned char);

				iview.set_size_request(width, height);

				Glib::signal_idle().connect(
						sigc::mem_fun(*this, &Cammer::loop)
						);

			} else {

				iview.stop_show();
				mem_free(rgb_data);

			}
		}

		Cammer::~Cammer() {
			//iview.stop_show();
			//con.disconnect();
			//mem_free(rgb_data);
		}

	}
}